# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/6/18

library(optparse)
library(magrittr)
library(ggrepel)
library(tidyverse)

createWhenNoExist <- function(f) {
  !dir.exists(f) && dir.create(f)
}

option_list <- list(
  make_option("--g", default = "SampleInfo.csv", type = "character", help = "sample group file"),
  make_option("--sc", default = "sample_color.txt", type = "character", help = "sample color file")
)
opt <- parse_args(OptionParser(option_list = option_list))

sampleInfo <- read_csv(opt$g) %>%
  select(c("SampleID", "ClassNote")) %>%
  mutate(ClassNote = factor(ClassNote, levels = unique(ClassNote)))

parent <- "./All_Combinaiton"
createWhenNoExist(parent)

sampleColDf <- read.csv(opt$sc, header = T, stringsAsFactors = F, comment.char = "") %>%
  select(c("ClassNote", "col"))
sampleCols <- sampleColDf %>%
  deframe()

pcDataFileName <- paste0(parent, "/../PCA_Score.csv")

pcData <- read_csv(pcDataFileName) %>%
  rename(SampleID = X1)

parameterFileName <- paste0(parent, "/../PCA_R2.csv")
parameterData <- read.csv(parameterFileName, header = T, stringsAsFactors = F, comment.char = "")

pNames <- pcData %>%
  column_to_rownames("SampleID") %>%
  colnames()

cn <- combn(pNames, 2)
for (i in 1:ncol(cn)) {
  row <- cn[, i]
  p1Name <- row[1]
  p2Name <- row[2]
  p1Index <- str_replace(p1Name, "PC", "")
  p2Index <- str_replace(p2Name, "PC", "")
  pc12 <- pcData %>%
    select(c("SampleID", p1Name, p2Name)) %>%
    set_colnames(c("SampleID", "p1", "p2")) %>%
    left_join(sampleInfo, by = c("SampleID"))
  impoPc1 <- parameterData[1, str_replace(p1Name, "p", "PC")]
  impoPc1 <- round(impoPc1 * 100, 2)
  impoPc2 <- parameterData[1, str_replace(p2Name, "p", "PC")]
  impoPc2 <- round(impoPc2 * 100, 2)
  head(print(pc12))
  p <- ggplot(pc12, mapping = aes(x = p1, y = p2, label = SampleID, color = ClassNote)) +
    xlab(paste0("PC", p1Index, "(", impoPc1, "%)", sep = "")) +
    ylab(paste0("PC", p2Index, "(", impoPc2, "%)", sep = "")) +
    theme_bw(base_size = 8.8, base_family = "Times") +
    theme(axis.text.x = element_text(size = 9, vjust = 0.5),
          axis.text.y = element_text(size = 8.8), legend.position = 'right',
          axis.title.y = element_text(size = 11), legend.margin = margin(t = 0.3, b = 0.1, unit = 'cm'),
          legend.text = element_text(size = 8), axis.title.x = element_text(size = 11),
          panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
    ) +
    #0 line
    geom_vline(aes(xintercept = 0), colour = "#BEBEBE", linetype = "solid") +
    geom_hline(aes(yintercept = 0), colour = "#BEBEBE", linetype = "solid") +
    #point
    stat_ellipse(aes(fill = ClassNote), colour = NA, size = 0.3, level = 0.95, type = "norm",
                 geom = "polygon", alpha = 0.1, show.legend = F) +
    geom_point(aes(colour = factor(ClassNote)), size = 4, stroke = 0) +
    geom_text_repel(segment.size = 0.2, size = 2, family = "Times") +
    #lengend
    scale_colour_manual("", values = sampleCols) +
    scale_fill_manual("", values = sampleCols)
  fileName <- paste0(parent, "/PC", p1Index, p2Index, "_Score_2D_Label.pdf")
  print("=plot before=")
  ggsave(limitsize = FALSE, fileName, p, width = 5, height = 4)
  print("=plot after=")
}
